package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.dto.RawMaterialDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.RawMaterialMapper;
import com.ruoyi.wms.domain.RawMaterial;
import com.ruoyi.wms.service.IRawMaterialService;

/**
 * 原材料主数据Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-05
 */
@Service
public class RawMaterialServiceImpl implements IRawMaterialService {
    @Autowired
    private RawMaterialMapper rawMaterialMapper;

    /**
     * 查询原材料主数据
     *
     * @param id 原材料主数据主键
     * @return 原材料主数据
     */
    @Override
    public RawMaterial selectRawMaterialById(String id) {
        return rawMaterialMapper.selectRawMaterialById(id);
    }

    /**
     * 查询原材料主数据列表
     *
     * @param rawMaterial 原材料主数据
     * @return 原材料主数据
     */
    @Override
    public List<RawMaterial> selectRawMaterialList(RawMaterialDto rawMaterial) {
        return rawMaterialMapper.selectRawMaterialList(rawMaterial);
    }

    /**
     * 新增原材料主数据
     *
     * @param rawMaterial 原材料主数据
     * @return 结果
     */
    @Override
    public int insertRawMaterial(RawMaterial rawMaterial) {

        rawMaterial.setId(MD5Utils.md5(rawMaterial.getMaterialCode()));
        rawMaterial.setCreateUser(SecurityUtils.getUsername());
        rawMaterial.setCreateDate(DateUtils.getCurrentTime());
        rawMaterial.setUpdateDate( DateUtils.getCurrentTime());
        return rawMaterialMapper.insertRawMaterial(rawMaterial);
    }

    /**
     * 修改原材料主数据
     *
     * @param rawMaterial 原材料主数据
     * @return 结果
     */
    @Override
    public int updateRawMaterial(RawMaterial rawMaterial) {
        int updateResult = 0;
        if (StringUtils.isEmpty(rawMaterial.getId())) {
            updateResult = insertRawMaterial(rawMaterial);
        }
        rawMaterial.setUpdateDate( DateUtils.getCurrentTime());
        updateResult = rawMaterialMapper.updateRawMaterial(rawMaterial);
        if (0 == updateResult) {
            updateResult = insertRawMaterial(rawMaterial);
        }

        return updateResult;


    }

    /**
     * 批量删除原材料主数据
     *
     * @param ids 需要删除的原材料主数据主键
     * @return 结果
     */
    @Override
    public int deleteRawMaterialByIds(String[] ids) {
        return rawMaterialMapper.deleteRawMaterialByIds(ids);
    }

    /**
     * 删除原材料主数据信息
     *
     * @param id 原材料主数据主键
     * @return 结果
     */
    @Override
    public int deleteRawMaterialById(String id) {
        return rawMaterialMapper.deleteRawMaterialById(id);
    }
}
